package com.tudouji.project.product.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.tudouji.common.constant.Constants;
import com.tudouji.common.enums.GeneralStatusEnums;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.poi.POIUtils;
import com.tudouji.framework.config.TuDouJiConfig;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.service.ISalesspecificationService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.product.mapper.InnerpriceMapper;
import com.tudouji.project.product.domain.Innerprice;
import com.tudouji.project.product.service.IInnerpriceService;

/**
 * 内部价格Service业务层处理
 * 
 * @author wf
 * @date 2020-12-30
 */
@Service
public class InnerpriceServiceImpl implements IInnerpriceService 
{
    @Autowired
    private InnerpriceMapper innerpriceMapper;

    @Autowired
    private TuDouJiConfig tuDouJiConfig;

    @Autowired
    private ISalesspecificationService salesspecificationService;

    /**
     * 查询内部价格
     * 
     * @param id 内部价格ID
     * @return 内部价格
     */
    @Override
    public Innerprice selectInnerpriceById(Long id)
    {
        return innerpriceMapper.selectInnerpriceById(id);
    }

    /**
     * 查询内部价格列表
     * 
     * @param innerprice 内部价格
     * @return 内部价格
     */
    @Override
    public List<Innerprice> selectInnerpriceList(Innerprice innerprice)
    {
        return innerpriceMapper.selectInnerpriceList(innerprice);
    }

    /**
     * 新增内部价格
     * 
     * @param innerprice 内部价格
     * @return 结果
     */
    @Override
    public AjaxResult insertInnerprice(Innerprice innerprice)
    {
        List<Innerprice> innerpriceList = new ArrayList<>();
        String url = innerprice.getTemplateUrl().replace(tuDouJiConfig.getApi()+ Constants.RESOURCE_PREFIX,tuDouJiConfig.getProfile());
        System.out.println(url);
        try{
            List<String[]> r = POIUtils.readExcelSheet(url, 0);
            for (String[] s : r) {
                Innerprice innere = new Innerprice();
                List<Salesspecification> salesspecifications = this.salesspecificationService.selectSalesspecificationBykingdeenum(s[0]);
                if(salesspecifications.size() == 0){
                    return AjaxResult.error(s[1]+"规格没有找到产品");
                }else{
                    for(Salesspecification salesspecification :salesspecifications ){
                        Innerprice i = new Innerprice();
                        i.setOrgguid(innerprice.getOrgguid());
                        i.setPurorgguid(innerprice.getPurorgguid());
                        i.setSpguid(salesspecification.getGuid());
                        Innerprice in = this.innerpriceMapper.checkInnerPrice(i);
                        if (in != null) {
                            if(in.getEffectivetime().getTime() > innerprice.getEffectivetime().getTime()){
                                return AjaxResult.error(s[1]+"规格下，有大于该生效时间的数据");
                            }
                        }

                        Innerprice ip = new Innerprice();
                        ip.setProductguid(salesspecification.getProductguid());
                        ip.setSpguid(salesspecification.getGuid());
                        ip.setOrgguid(innerprice.getOrgguid());
                        ip.setPurorgguid(innerprice.getPurorgguid());
                        ip.setStatus(innerprice.getStatus());
                        ip.setEffectivetime(innerprice.getEffectivetime());
                        ip.setCreatedby(SecurityUtils.getLoginUser().getUser().getUserId().toString());
                        ip.setCreatetime(DateUtils.getNowDate());

                        ip.setLastupdatetime(DateUtils.getNowDate());

                        ip.setPrice(new BigDecimal(s[2]));
                        innerpriceList.add(ip);
                    }

                }
//                else {
//                    List<Salesspecification> list = salesspecifications.stream().filter(m -> s[1].equals(m.getName())).collect(Collectors.toList());
//                    if(list.size() == 0){
//                        return AjaxResult.error(s[1]+"规格没有找到产品");
//                    }else if(list.size() == 1){
//                        Innerprice i = new Innerprice();
//                        i.setOrgguid(innerprice.getOrgguid());
//                        i.setPurorgguid(innerprice.getPurorgguid());
//                        i.setSpguid(list.get(0).getGuid());
//                        Innerprice in = this.innerpriceMapper.checkInnerPrice(i);
//                        if (in != null) {
//                            if(in.getEffectivetime().getTime() > innerprice.getEffectivetime().getTime()){
//                                return AjaxResult.error(s[1]+"规格下，有大于该生效时间的数据");
//                            }
//                        }
//                        innere.setProductguid(list.get(0).getProductguid());
//                        innere.setSpguid(list.get(0).getGuid());
//                    }else{
//                        return AjaxResult.error(s[1]+"规格有多个正常的产品，无法导入");
//                    }
//                }
                System.out.println(s[1]);
                System.out.println(s[0]);



            }
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
        this.innerpriceMapper.insertInnerpriceBath(innerpriceList);
        return AjaxResult.success();
        //return innerpriceMapper.insertInnerprice(innerprice);
    }

    /**
     * 修改内部价格
     * 
     * @param innerprice 内部价格
     * @return 结果
     */
    @Override
    public AjaxResult updateInnerprice(Innerprice innerprice)
    {
        Innerprice i = new Innerprice();
        i.setOrgguid(innerprice.getOrgguid());
        i.setPurorgguid(innerprice.getPurorgguid());
        i.setSpguid(innerprice.getSpguid());
        Innerprice in = this.innerpriceMapper.checkInnerPrice(i);
        if (in != null) {
            if(in.getEffectivetime().getTime() > innerprice.getEffectivetime().getTime()){
                return AjaxResult.error("有大于该生效时间的数据");
            }
        }
        int z = innerpriceMapper.updateInnerprice(innerprice);
        if(z > 0){
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 批量删除内部价格
     * 
     * @param ids 需要删除的内部价格ID
     * @return 结果
     */
    @Override
    public int deleteInnerpriceByIds(Long[] ids)
    {
        return innerpriceMapper.deleteInnerpriceByIds(ids);
    }

    /**
     * 删除内部价格信息
     * 
     * @param id 内部价格ID
     * @return 结果
     */
    @Override
    public int deleteInnerpriceById(Long id)
    {
        return innerpriceMapper.deleteInnerpriceById(id);
    }
}
